package com.gxa.admin.recycling.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.admin.recycling.mapper.*;
import com.gxa.admin.recycling.service.RecycleOrderService;
import com.gxa.admin.recycling.service.RecycleitemService;
import com.gxa.core.recycling.pojo.*;
import com.gxa.core.recycling.service.impl.BaseServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service("recycleOrderService")
public class RecycleOrderServiceImpl extends BaseServiceImpl<Recycleorder> implements RecycleOrderService {
    @Autowired
    RecycleOrderMapper recycleOrderMapper;

    @Autowired
    WasteMapper wasteMapper;

    @Autowired
    WasteTypeMapper wasteTypeMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CommunityManageMapper communityManageMapper;

    @Autowired
    RecyclerMapper recyclerMapper;

    @Autowired
    RecycleitemService recycleitemService;

    @Override
    public List<Map> findAllByPage(Long id, String comityname, String provinces, String username, String recyclername, String state, String recoverytime, Integer page, Integer size) {
        if (comityname!=null && comityname !=""){
            comityname = "%"+comityname+"%";
        }
        if (provinces!=null && provinces !=""){
            provinces = "%"+provinces+"%";
        }
        if (username!=null && username !=""){
            username = "%"+username+"%";
        }
        if (recyclername!=null && recyclername !=""){
            recyclername = "%"+recyclername+"%";
        }
        if (recoverytime!=null && recoverytime !=""){
            recoverytime = "%"+recoverytime+"%";
        }

        return recycleOrderMapper.findAllByPage(id, comityname, provinces, username, recyclername, state, recoverytime, (page-1)*size, size);
    }

    @Override
    public List<Map> findById(Long id) {
        return recycleOrderMapper.findById(id);
    }

    @Override
    public Map<Integer,List<Map>> findAll(Long id, Long communityid,String userName, String recyclerName, String state, String time,Integer page, Integer size) throws IllegalAccessException {

        Map<Integer,List<Map>> map=new HashMap<>();

        if(userName!=null&& userName !=""){
            userName = "%"+userName+"%";
        }
        if(recyclerName!=null&& recyclerName !=""){
            recyclerName = "%"+recyclerName+"%";
        }
        if(state!=null&& state !=""){
            state = "%"+state+"%";
        }
        if(time!=null&& time !=""){
            time = "%"+time+"%";
        }
        List<Map> all = recycleOrderMapper.findAll(id,communityid,userName, recyclerName, state, time, (page - 1) * size, size);
        Integer total = recycleOrderMapper.findAll(id,  communityid,userName, recyclerName, state, time, null, null).size();

        map.put(total,all);
        return map;

    }


    @Override
    public RecycleOrderShowVo findRecycleOrderDetailById(Long id) {
        RecycleOrderShowVo recycleOrderShowVo=new RecycleOrderShowVo();
        Recycleorder recycleorder = recycleOrderMapper.selectByPrimaryKey(id);
        List<Recycleitem> recycleitems = recycleitemService.findRecycleitemByOrderId(id);
        List<RecycleItemVo> recycleItemVoList=new ArrayList<>();
        for (Recycleitem recycleitem : recycleitems) {
            RecycleItemVo vo=new RecycleItemVo();
            Waste waste = wasteMapper.selectByPrimaryKey(recycleitem.getFid());

            vo.setTypeName(wasteTypeMapper.selectByPrimaryKey(waste.getWasteTypeId()).getTypeName());

            vo.setWasteTypeName(waste.getName());
            vo.setWeight(recycleitem.getWeight());
            recycleItemVoList.add(vo);
        }
        //得到用户信息
        Long uid1 = recycleorder.getUid();
        User user = userMapper.selectByPrimaryKey(uid1);
        recycleOrderShowVo.setUserName(user.getName());
        recycleOrderShowVo.setUserPhone(user.getPhone());
        recycleOrderShowVo.setLocation(user.getLocation());
        //得到订单地址信息
        Long did1 = recycleorder.getDid();
        CommunityManage communityManage = communityManageMapper.selectByPrimaryKey(did1);
        recycleOrderShowVo.setProvinces(communityManage.getProvinces());
        //得到回收员信息
        Long hid1 = recycleorder.getHid();
        Recycler recycler = recyclerMapper.selectByPrimaryKey(hid1);
        recycleOrderShowVo.setRecycleName(recycler.getName());
        //得到订单其他信息
        recycleOrderShowVo.setId(recycleorder.getId());
        recycleOrderShowVo.setRecoverytime(recycleorder.getRecoverytime());
        recycleOrderShowVo.setState(recycleorder.getState());

        recycleOrderShowVo.setList(recycleItemVoList);
        return recycleOrderShowVo;

    }

    @Override
    public void updateRecycleOrder(Recycleorder recycleorder) {

        //入库操作,增加库存
        Long id = recycleorder.getId();
        String state = recycleorder.getState();

        Set<String> stringSet=new HashSet<>();
        if(state.equals("已入库")){
            List<Recycleitem> recycleitems = recycleitemService.findRecycleitemByOrderId(id);

            for (Recycleitem recycleitem : recycleitems) {
                Long fid = recycleitem.getFid();
                Waste waste = wasteMapper.selectByPrimaryKey(fid);
                Integer weight = recycleitem.getWeight();

                System.out.println(waste);
                waste.setRemainNum(waste.getRemainNum()+weight);
                wasteMapper.updateByPrimaryKeySelective(waste);

                Long wasteTypeId = wasteMapper.selectByPrimaryKey(fid).getWasteTypeId();
                WasteType wasteType = wasteTypeMapper.selectByPrimaryKey(wasteTypeId);
                String typeName = wasteType.getTypeName();

                stringSet.add(typeName);
            }
        }
        //利用set去重,设置入库订单的物资名称
        StringBuffer stringBuffer=new StringBuffer();
        for (String s : stringSet) {
            stringBuffer.append(s).append(",");
        }
        String s = stringBuffer.toString();
        recycleorder.setTitle(s.substring(0,s.length()-1));

        recycleOrderMapper.updateByPrimaryKeySelective(recycleorder);

    }
}
